# Maintainer: Yang Zheng <yz757865041@gmail.com>
# Contributors: Marcin Sielski <marcin.sielski(at)gmail.com>

_realname=paho.mqtt.c
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.3.9
pkgrel=1
pkgdesc="Eclipse Paho MQTT C client (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://eclipse.org/paho/clients/c/'
license=('EPL/EDL')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
			 "${MINGW_PACKAGE_PREFIX}-ninja"
			 "${MINGW_PACKAGE_PREFIX}-openssl"
       "${MINGW_PACKAGE_PREFIX}-cc"
			 "unzip")
optdepends=("${MINGW_PACKAGE_PREFIX}-openssl")
options=('strip' 'staticlibs')
source=("${_realname}-${pkgver}.zip"::"https://github.com/eclipse/${_realname}/archive/v${pkgver}.zip")
sha256sums=('78AEB3E114D3550C5285D7C4C805135E4AAEF0E43B278A4797E590FA58D10D06')
noextract=("${_realname}-${pkgver}.zip")

prepare() {
  unzip "${_realname}-${pkgver}.zip"
  cd "${srcdir}"/${_realname}-${pkgver}
}

build() {
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}

  CFLAGS+=" -D_WINDOWS"
  CXXFLAGS+=" -D_WINDOWS"  
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G'Ninja' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DPAHO_BUILD_STATIC=ON \
      -DPAHO_WITH_SSL=TRUE \
	  -DPAHO_BUILD_DOCUMENTATION=FALSE \
	  -DPAHO_BUILD_SAMPLES=TRUE \
      -S ../${_realname}-${pkgver} \
	  -B ./

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}


package() {
  cd "${srcdir}"/build-${CARCH}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
